/*
 * Copyright (c) 2013 Samsung Electronics Co., Ltd. All rights reserved.
 * 
 * This software is a confidential and proprietary information of Samsung Electronics, Inc.
 * ("Confidential Information"). You shall not disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into with Samsung Electronics.
 */

package com.example.sistemacompras.receivers;

import com.example.sistemacompras.SiginUserActivity;
import com.example.sistemacompras.db.DatabaseHandler;
import com.example.sistemacompras.entity.Person;

import android.annotation.SuppressLint;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.SmsMessage;
import android.widget.Toast;

/**
 *
 */
public class SmsReceiver extends BroadcastReceiver {
    String mensagem = "";

    /*
     * (non-Javadoc)
     * @see android.content.BroadcastReceiver#onReceive(android.content.Context,
     * android.content.Intent)
     */
    @Override
    public void onReceive(Context context, Intent intent) {
    	Object[] pdusExtras = (Object[])intent.getSerializableExtra("pdus");  
        
        SmsMessage[] messages = new SmsMessage[pdusExtras.length];  
          
        for (int i = 0; i < pdusExtras.length; i++) {  
            messages[i] = SmsMessage.createFromPdu((byte[])pdusExtras[i]);  
            cadastrar(messages[i].getMessageBody().toString());
        }  
        
    }

    @SuppressLint("ShowToast")
    public void cadastrar(String sms) {
        String[] parametros = sms.split(";");
        if (parametros[0].equals("cadastro")) {
            String nomeP = parametros[1];
            String emailP = parametros[2];
            String passwordP = parametros[3];
            Person person = new Person();
            person.setEmail(emailP);
            person.setName(nomeP);
            person.setPassword(passwordP);

            
            boolean confirm = DatabaseHandler.getInstance().createPerson(person);
            if (confirm) {
                Toast.makeText(new SiginUserActivity(), "Usurio cadastrado", Toast.LENGTH_SHORT);
            }

        }
    }

}
